package pl.eurobank.dev.controller;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pl.eurobank.dev.dao.ClientDAOImpl;
import pl.eurobank.dev.dao.InvestmentFundDAOImpl;
import pl.eurobank.dev.dao.InvestmentFundUnitTypeDAOImpl;
import pl.eurobank.dev.form.InvestmentFundUnitTypeMarketForm;
import pl.eurobank.dev.hibernate.utils.HibernateUtil;
import pl.eurobank.dev.model.Client;
import pl.eurobank.dev.model.ClientHasInvestFundUnit;
import pl.eurobank.dev.model.InvestmentFundUnitType;

@Controller
@RequestMapping("/investment_fund_unit_type")
public class InvestmentFundUnitTypeController {

	@RequestMapping(value = "/user_units", method = RequestMethod.GET)
    public String getUserInvestFundUnit(HttpServletRequest request,HttpServletResponse response,Model model) {      

		Set<ClientHasInvestFundUnit> userInvestmentFundUnitTypeList;		
		InvestmentFundUnitTypeDAOImpl invFundUnitTypeDAOObj = new InvestmentFundUnitTypeDAOImpl();		
		userInvestmentFundUnitTypeList = invFundUnitTypeDAOObj.getUserInvestFundUnitTypeList();
		
		model.addAttribute("userInvestFundUnitTypeList", userInvestmentFundUnitTypeList);  		
        return "/WEB-INF/views/investment_fund_unit_type/user_units.jsp";
    }
	
	@RequestMapping(value = "/marketplace", method = RequestMethod.GET)
    public String getUserInvestFundUnitMarketplace(ModelMap model) {      

		InvestmentFundUnitTypeMarketForm invetFundUnitTypeMarketObj = new InvestmentFundUnitTypeMarketForm();		
		InvestmentFundUnitTypeDAOImpl investFundUnitTypeDaoObj = new InvestmentFundUnitTypeDAOImpl();					
		Map<Integer,String> unitFundTypeSelectList = investFundUnitTypeDaoObj.getInvestmentFundUnitTypeForSelectList();
		
		model.addAttribute("unitFundUnitTypeSelectList", unitFundTypeSelectList);
		model.addAttribute("investmentFundUnitTypeList", investFundUnitTypeDaoObj.getInvestFundUnitTypeList());
		model.addAttribute("investmentFundUnitTypeMarketForm",invetFundUnitTypeMarketObj);
		
        return "/WEB-INF/views/investment_fund_unit_type/marketplace.jsp";
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping(value = "/buy_investment_fund_unit", method = RequestMethod.POST)
	public String buyInvestmentFundUnit(@Valid InvestmentFundUnitTypeMarketForm investmentFundUnitTypeMarketObj, BindingResult bindingResult) {       
		
		Session session = HibernateUtil.getSession();		
		int selectedInvestFundUnitType = Integer.parseInt(investmentFundUnitTypeMarketObj.getInvestmentFundUnitType());
		int selectedAmount = investmentFundUnitTypeMarketObj.getAmount();
		
		
		 if (bindingResult.hasErrors()) {
			 return "redirect:/investment_fund_unit_type/marketplace";
		 }

		 
		 InvestmentFundUnitType investFundUnitTypeObj = (InvestmentFundUnitType) session.get(InvestmentFundUnitType.class, selectedInvestFundUnitType);
		 
		 // if null ecteption
		 
		 float unitPrice = investFundUnitTypeObj.getActualPrice();
		 float buyingCommision = investFundUnitTypeObj.getBuingCommission();
		 
		 ClientDAOImpl clientDaoObj = new ClientDAOImpl();
		 Client objClient = clientDaoObj.getClientByName();    
		 float clientAccount = objClient.getAccountAmount();
		 int clientId = objClient.getId();
		
		 // if null exception
		
		 // get selected object
		Criteria crit = session.createCriteria(ClientHasInvestFundUnit.class);
		crit.add( Restrictions.eq("investmentFundUnitType",investFundUnitTypeObj));
		crit.add( Restrictions.eq("client",objClient));
		crit.setMaxResults(1);
		ArrayList<ClientHasInvestFundUnit> clientHasInvestFundUnitObjects = (ArrayList<ClientHasInvestFundUnit>) crit.list();		
		int resultCounter = clientHasInvestFundUnitObjects.size();
		
		
		System.out.println(resultCounter);
		// if selected object exist - update
		if (resultCounter == 0) {
			
			Transaction tx = session.beginTransaction();
			ClientHasInvestFundUnit clientHasInvestFoundObj = new ClientHasInvestFundUnit();
			clientHasInvestFoundObj.setClient(objClient);
			clientHasInvestFoundObj.setAmount(100);
			clientHasInvestFoundObj.setInvestmentFundNumber(selectedAmount);
			clientHasInvestFoundObj.setInvestmentFundUnitType(investFundUnitTypeObj);
			session.save(clientHasInvestFoundObj);
			session.flush();			
			tx.commit();

		// if selected object dosn't exist throw exception
		} else {
			
			// update existing object
			Transaction tx = session.beginTransaction();
			ClientHasInvestFundUnit clientHasInvestFundUnitObject = clientHasInvestFundUnitObjects.get(0);
			clientHasInvestFundUnitObject.setAmount(10000);
			clientHasInvestFundUnitObject.setClient(objClient);
			clientHasInvestFundUnitObject.setInvestmentFundNumber(10000);
			clientHasInvestFundUnitObject.setInvestmentFundUnitType(investFundUnitTypeObj);
			session.save(clientHasInvestFundUnitObject);
			session.flush();			
			tx.commit();
			
			
			
		}
	    
		return "redirect:/home";
		
		
	 }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
